import React from 'react'
import PropTypes from 'prop-types'
import { Router } from 'dva/router'
import App from './routes/app'

const registerModel = (app, model) => {
  if (!(app._models.filter(m => m.namespace === model.namespace).length === 1)) {
    app.model(model)
  }
}

const Routers = function ({ history, app }) {
  const routes = [
    {
      path: '/',
      component: App,
      getIndexRoute (nextState, cb) {
        require.ensure([], require => {
          registerModel(app, require('./models/dashboard'))
          cb(null, { component: require('./routes/dashboard/') })
        }, 'dashboard')
      },
      childRoutes: [
        {
          path: 'dashboard',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/dashboard'))
              cb(null, require('./routes/dashboard/'))
            }, 'dashboard')
          },
        }, {
          path: 'configureList/productConfigList',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/productConfig'))
              cb(null, require('./routes/productConfigList/'))
            }, '产品配置')
          },
        },{
          path: 'configureList/productConfigList/id',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/productConfig/detail'))
              cb(null, require('./routes/productConfigList/detail/'))
            }, '产品详情页')
          },
        },

        {
            path: 'configureList/moduleConfigList',
            getComponent (nextState, cb) {
              require.ensure([], require => {
                registerModel(app, require('./models/moduleConfig'))
                cb(null, require('./routes/moduleConfigList/'))
              }, '功能模块配置')
            },
          },{
            path: 'configureList/moduleConfigList/id',
            getComponent (nextState, cb) {
              require.ensure([], require => {
                registerModel(app, require('./models/moduleConfig/detail'))
                cb(null, require('./routes/moduleConfigList/detail/'))
              }, '功能模块详情页')
            },
          }
        ,
        {
          path: 'configureList/testHostConfigList',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testHostConfig'))
              cb(null, require('./routes/testHostConfigList/'))
            }, '测试机配置')
          },
        },{
          path: 'configureList/testHostConfigList/id',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testHostConfig/detail'))
              cb(null, require('./routes/testHostConfigList/detail/'))
            }, '测试机详情页')
          },
        },{
          path: 'configureList/sutConfigList',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/sutConfig'))
              cb(null, require('./routes/sutConfigList/'))
            }, '服务机配置')
          },
        },{
          path: 'configureList/sutConfigList/id',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/sutConfig/detail'))
              cb(null, require('./routes/sutConfigList/detail/'))
            }, '服务机详情页')
          },
        },{
          path: 'configureList/testSuiteConfigList',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testSuiteConfig'))
              cb(null, require('./routes/testSuiteConfigList/'))
            }, '测试套件配置')
          },
        },{
          path: 'configureList/testSuiteConfigList/id',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testSuiteConfig/detail'))
              cb(null, require('./routes/testSuiteConfigList/detail/'))
            }, '测试套件详情页')
          },
        },{
          path: 'configureList/testPlanConfigList',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testPlanConfig'))
              cb(null, require('./routes/testPlanConfigList/'))
            }, '测试计划配置')
          },
        },{
          path: 'configureList/testPlanConfigList/id',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testPlanConfig/detail'))
              cb(null, require('./routes/testSuiteConfigList/detail/'))
            }, '测试计划详情页')
          },
        },{
          path: 'user',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/user'))
              cb(null, require('./routes/user/'))
            }, 'user')
          },
        }, {
          path: 'user/:id',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/user/detail'))
              cb(null, require('./routes/user/detail/'))
            }, 'user-detail')
          },
        }, {
          path: 'login',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/login'))
              cb(null, require('./routes/login/'))
            }, 'login')
          },
        }, {
          path: 'request',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/request/'))
            }, 'request')
          },
        }, {
          path: 'UIElement/iconfont',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/UIElement/iconfont/'))
            }, 'UIElement-iconfont')
          },
        }, {
          path: 'UIElement/search',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/UIElement/search/'))
            }, 'UIElement-search')
          },
        }, {
          path: 'UIElement/dropOption',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/UIElement/dropOption/'))
            }, 'UIElement-dropOption')
          },
        }, {
          path: 'UIElement/layer',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/UIElement/layer/'))
            }, 'UIElement-layer')
          },
        }, {
          path: 'UIElement/dataTable',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/UIElement/dataTable/'))
            }, 'UIElement-dataTable')
          },
        }, {
          path: 'UIElement/editor',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/UIElement/editor/'))
            }, 'UIElement-editor')
          },
        }, {
          path: 'chart/lineChart',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/chart/lineChart/'))
            }, 'chart-lineChart')
          },
        }, {
          path: 'chart/barChart',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/chart/barChart/'))
            }, 'chart-barChart')
          },
        }, {
          path: 'chart/areaChart',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/chart/areaChart/'))
            }, 'chart-areaChart')
          },
        }, {
          path: 'post',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/post/'))
            }, '监控')
          },
        },
        // {
        //   path: 'post',
        //   getComponent (nextState, cb) {
        //     require.ensure([], require => {
        //       registerModel(app, require('./models/post'))
        //       cb(null, require('./routes/post/'))
        //     }, 'post')
        //   },
        // },
        {
          path: 'testResult',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testResult'))
              cb(null, require('./routes/testResult/'))
            }, '测试结果')
          },
        }, {
          path: 'execute',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/execute'))
              cb(null, require('./routes/execute/'))
            }, '测试执行')
          },
        },
        {
          path: 'getSpecificResult/',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/specificResult'))
              cb(null, require('./routes/specificResult/'))
            }, '邮件测试结果')
          },
        },
        {
          path: '*',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/error/'))
            }, 'error')
          },
        },
      ],
    },
  ]

  return <Router history={history} routes={routes} />
}

Routers.propTypes = {
  history: PropTypes.object,
  app: PropTypes.object,
}

export default Routers
